package com.bytz.modules.cms.productLine.service.impl;

import com.bytz.common.exception.BytzBootException;
import com.bytz.common.system.base.service.impl.AbstractCrudService;
import com.bytz.common.system.vo.LoginUser;
import com.bytz.common.util.SecurityUtils;
import com.bytz.modules.cms.productLine.constants.ProductLineConstants;
import com.bytz.modules.cms.productLine.entity.ProductLine;
import com.bytz.modules.cms.productLine.mapper.ProductLineMapper;
import com.bytz.modules.cms.productLine.service.IAdminProductLineService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: cms-backend
 * @BelongsPackage: com.bytz.modules.cms.productLine.service.impl
 * @Author: dst
 * @CreateTime: 2022-10-27  14:13
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class AdminProductLineServiceImpl extends AbstractCrudService<ProductLineMapper, ProductLine, ProductLine> implements IAdminProductLineService {
    @Override
    protected ProductLine insertSaveObject(ProductLine saveRo) {
        if (lambdaQuery().eq(ProductLine::getDeptId, saveRo.getDeptId()).exists()) {
            throw new BytzBootException(String.format("部门【%s】已绑定至其他产品线", saveRo.getDeptName()));
        }
        save(saveRo);
        return saveRo;
    }

    @Override
    protected ProductLine updateSaveObject(ProductLine saveRo) {
        if (lambdaQuery()
                .eq(ProductLine::getDeptId, saveRo.getDeptId())
                .ne(ProductLine::getId, saveRo.getId())
                .exists()) {
            throw new BytzBootException(String.format("部门【%s】已绑定至其他产品线", saveRo.getDeptName()));
        }
        updateById(saveRo);
        return saveRo;
    }

    @Override
    protected ProductLine saveOthers(ProductLine saveRo) {
        return null;
    }

    @Override
    protected boolean deleteEntity(ProductLine entity) {
        return removeById(entity.getId());
    }

    @Override
    protected void deleteOthers(ProductLine entity) {

    }

    @Override
    public List<ProductLine> listByLoginUser() {
        if (SecurityUtils.hasAuthority(ProductLineConstants.Permissions.VIEW_ALL_LINE)) {
            return list();
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return lambdaQuery().eq(ProductLine::getDeptId, loginUser.getDeptId()).list();
    }
}
